<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<style type="text/css" rel="stylesheet" href="style.css">
	

	</style>
	<title>OnSipCaller</title>
	<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js" type="text/javascript"></script>

	<script type="text/javascript">
		// the URL of the Junction Networks API
		var jn_apiUrl = "http://www.jnctn.com/restapi";
		var defaultFromAddress;
		var defaultToDomain;
		var aObj;

		//
		// JSONscriptRequest 
		//

		// Constructor -- pass a REST request URL to the constructor
		function JSONscriptRequest(fullUrl) {
			// REST request path
			this.fullUrl = fullUrl; 
			// Keep IE from caching requests
			this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
			// Get the DOM location to put the script tag
			this.headLoc = document.getElementsByTagName("head").item(0);
			// Generate a unique script tag id
			this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
		}

		// Static script ID counter
		JSONscriptRequest.scriptCounter = 1;

		// buildScriptTag method
		//
		JSONscriptRequest.prototype.buildScriptTag = function () {

			// Create the script tag
			this.scriptObj = document.createElement("script");
			
			// Add script object attributes
			this.scriptObj.setAttribute("type", "text/javascript");
			this.scriptObj.setAttribute("charset", "utf-8");
			this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
			this.scriptObj.setAttribute("id", this.scriptId);
		}
		 
		// removeScriptTag method
		// 
		JSONscriptRequest.prototype.removeScriptTag = function () {
			// Destroy the script tag
			this.headLoc.removeChild(this.scriptObj);  
		}

		// addScriptTag method
		//
		JSONscriptRequest.prototype.addScriptTag = function () {
			// Create the script tag
			this.headLoc.appendChild(this.scriptObj);
		}

		function FixNumberFormat(number){
			var numberToCall;
			
			if(number.indexOf("@") != -1) return number;
			
			numberToCall = number.replace(/\.|-|\s|\(|\)|\\|\//mgi, "");
									
			// add one for ten digit numbers
			if(numberToCall.substring(1, 0) != "1" && numberToCall.length === 10) {
				numberToCall = "1" + numberToCall;
			}
				
			return numberToCall + defaultToDomain;
		}

		$(document).ready(function () {
			loadGadget();
			
			$('#makeCall').click(function() {
				var toAddress = FixNumberFormat($('#NumberToCall').val());
				var query = 
					"Action=CallSetup&Output=json&Callback=callbackFunc" +
					"&FromAddress=" + defaultFromAddress +
					"&ToAddress=" 	+ toAddress;
				
				$('#debug').html(toAddress);
				
				var request = jn_apiUrl + '?' + query;
				
				aObj = new JSONscriptRequest(request);
				  aObj.buildScriptTag();
				  aObj.addScriptTag();
			});
		});

		function loadGadget() {
			defaultFromAddress = System.Gadget.Settings.readString("defaultFromAddress");
			defaultToDomain = System.Gadget.Settings.readString("defaultToDomain");
			
			$('#debug').html(defaultFromAddress + "<br />" + defaultToDomain);
			
			System.Gadget.settingsUI = "settings.html";
			System.Gadget.onSettingsClosed = loadGadget;
		}

		function callbackFunc(jsonData) {
			// check for a valid response
			if(!jsonData.Response) {
				alert("Error: Invalid Response - " + jsonData.toString());
			}
			else {
				var isValid = jsonData.Response.Context.Request.IsValid;
				var isCompleted = jsonData.Response.Context.Action.IsCompleted;
				
				if(isValid && isCompleted) {
					$('#NumberToCall').val(""); // reset textbox
				}
			}

			// remove the script tag
			aObj.removeScriptTag();

			return;
		}
	</script>
</head>

<body>
	<div class="gadgetContainer">
		<p>Enter the number to call:</p>
		<input type="text" id="NumberToCall" />
		<input type="button" value="Call" id="makeCall"/>
	</div>
</body>

</html>